﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public class TreeHelper
    {
        /// <summary>
        /// 根据父节点的选择情况，设制子节点选择
        /// </summary>
        /// <param name="treeNode"></param>
        /// <param name="bCh"></param>
        public static void CheckChildrenBySelf(TreeNode treeNode, bool bCh)
        {
            TreeNodeCollection nodes = treeNode.Nodes;
            foreach (TreeNode n in nodes)
            {
                n.Checked = bCh;
                CheckChildrenBySelf(n, bCh);
            }
        }

        /// <summary>
        /// 获得所有叶子节点（只能获得两级子节点）
        /// </summary>
        /// <returns></returns>
        public static List<TreeNode> GetAllYzTreeNode(TreeNode treeNode)
        {
            List<TreeNode> lstTreeNode = new List<TreeNode>();
            List<TreeNode> lstTreeNode2 = new List<TreeNode>();
            foreach (TreeNode node in treeNode.Nodes)
            {
                lstTreeNode2.Add(node);
            }
            for (int i = 0; i < lstTreeNode2.Count; i++)
            {
                if (lstTreeNode2[i].Nodes.Count == 0)
                {
                    lstTreeNode.Add(lstTreeNode2[i]);
                }
                else
                {
                    foreach (TreeNode node in lstTreeNode2[i].Nodes)
                    {
                        lstTreeNode2.Add(node);
                    }
                }
            }
            return lstTreeNode;
        }

        /// <summary>
        /// 获得所有叶子节点
        /// </summary>
        /// <returns></returns>
        public static List<TreeNode> GetAllYzTreeNode2(TreeNode treeNode)
        {
            List<TreeNode> lstTreeNode = new List<TreeNode>();
            //List<TreeNode> lstTreeNode2 = new List<TreeNode>();
            if (treeNode.Nodes.Count == 0)
            {
                lstTreeNode.Add(treeNode);
                return lstTreeNode;
            }

            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Nodes.Count == 0)
                {
                    lstTreeNode.Add(node);
                }
                else
                {
                    foreach (TreeNode node2 in node.Nodes)
                    {
                        lstTreeNode.AddRange(GetAllYzTreeNode2(node2));
                    }
                }
            }


            //foreach (TreeNode node in treeNode.Nodes)
            //{
            //    lstTreeNode2.Add(node);
            //}
            //for (int i = 0; i < lstTreeNode2.Count; i++)
            //{
            //    if (lstTreeNode2[i].Nodes.Count == 0)
            //    {
            //        lstTreeNode.Add(lstTreeNode2[i]);
            //    }
            //    else
            //    {
            //        foreach (TreeNode node in lstTreeNode2[i].Nodes)
            //        {
            //            lstTreeNode2.AddRange(GetAllYzTreeNode2(node));
            //        }
            //    }
            //}
            return lstTreeNode;



        }


        /// <summary>
        /// 设置父节点状态（如果同级节点全为选中状态，则父节点也为选中状态；否则父节点为未选状态）
        /// </summary>
        /// <param name="treeNode"></param>
        public static void SetParentNodeState(TreeNode treeNode)
        {
            if (treeNode.Parent == null) return;

            TreeNode tNodeParent = treeNode.Parent;

            tNodeParent.Checked = true;
            foreach (TreeNode tNodeSub in tNodeParent.Nodes)
            {
                if (tNodeSub.Checked == false)
                {
                    tNodeParent.Checked = false;
                    break;
                }
            }

            SetParentNodeState(tNodeParent);

        }

        /// <summary>
        /// 获得某叶子节点的顶级节点
        /// </summary>
        /// <param name="treeNode"></param>
        /// <returns></returns>
        public static TreeNode GetTopTreeNode(TreeNode treeNode)
        {
            if (treeNode.Parent != null)
            {
                return GetTopTreeNode(treeNode.Parent);
            }
            else
            {
                return treeNode;
            }
        }
    }
}
